package com.lrs.java.dataStructure.simpleSort;

class InsertArray {

	private long[] arr;
	private int nElems;
	
	public InsertArray(int capacity) {
		arr = new long[capacity];
	}

	public int size() {
		return nElems;
	}

	public void insert(int value) {
		arr[nElems] = value;
		nElems++;
	}
	
	
	public void insertSort() {
		int out, in;	
		long temp;
		for(out=1; out<nElems; out++) {
			temp = arr[out];
			
			in = out-1;
			while(in>=0 && arr[in]>temp) {
				arr[in+1] = arr[in];
				in--;
			}
			
//			if(in != out-1) {
				arr[in+1] = temp;
//			}
		}
	}
	
	public void display() {
		for(int i=0; i<nElems; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
	
}
class InsertSortApp {
	public static void main(String[] args) {
		
		InsertArray arr = new InsertArray(10);
		
		arr.insert(10);
		arr.insert(70);
		arr.insert(0);
		arr.insert(34);
		arr.insert(77);
		arr.insert(0);
		arr.insert(40);
		
		arr.display();
		
		arr.insertSort();
		
		arr.display();
		
	}
}
